package cn.vw.hi.user.persistence;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Expression;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import cn.vw.hi.commen.tools.Page;
import cn.vw.hi.user.model.User;

public class UserDaoImpl extends HibernateDaoSupport implements UserDao {

	//添加用户
	public boolean addUser(User user) {
		try
		{
			this.getHibernateTemplate().save(user);
		}catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}
		return true;
	}
	//修改用户
	public boolean modifyUser(User user) {
	    try
	    {
	    	User u=(User)this.getHibernateTemplate().get(User.class, user.getId());
	    	u.setPassword(user.getPassword());
	    	this.getHibernateTemplate().update(u);
	    	user=u;
	    }catch(Exception e)
	    {
	    	e.printStackTrace();
	    	return false;
	    }
		return true;
	}
	//删除用户
	public boolean deleteUser(User user) {
		try
		{
			User u=(User)this.getHibernateTemplate().get(User.class,user.getId());
			this.getHibernateTemplate().delete(u);
		}catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}
		return true;
	}
	//查找用户
	public boolean findUser(final User user) {
		try
		{
		    User u=(User)this.getHibernateTemplate().execute(new HibernateCallback(){

				public Object doInHibernate(Session session)
						throws HibernateException, SQLException {
					Criteria criteria=session.createCriteria(User.class);
					         criteria.add(Expression.eq("email",user.getEmail()));
					return criteria.uniqueResult();
				}
				
			});
		    if(u==null)
		    {
		    	return false;
		    }
		    else
		    {
		    	return true;
		    }
		}catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}
	}
	//用户登录
	public User loginUser(final User user) {
		User u=(User)this.getHibernateTemplate().execute(new HibernateCallback(){
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria criteria=session.createCriteria(User.class);
				criteria.add(Expression.eq("email", user.getEmail()));
				criteria.add(Expression.eq("password", user.getPassword()));
				return criteria.uniqueResult();
			}
		});
		return u;
	}
	//获得用户列表
	public List userList(final Page pageObj) {
		List userList=(List)this.getHibernateTemplate().execute(new HibernateCallback(){

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria criteria=session.createCriteria(User.class);
				         criteria.setFirstResult((pageObj.getCurrentPage()-1)*pageObj.getPageSize());
				         criteria.setMaxResults(pageObj.getPageSize());
				         int rowCount = getRowCount("from User user");
				         pageObj.setRowCount(rowCount);
				         pageObj.setTotalPage(rowCount);
				         pageObj.setPreNextPage();
				return criteria.list();
			}
			
		});
		return userList;
	}
	/**
	 * 获得记录数
	 */
	public int getRowCount(final String hql) {
		Integer rowCount = 0;
		rowCount = (Integer) this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						return session.createQuery(hql).list().size();
					}
				});
		return rowCount;
	}
	//查询用户
	public User findUserByEmail(final User user) {
		User u=(User)this.getHibernateTemplate().execute(new HibernateCallback(){

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria criteria=session.createCriteria(User.class);
				criteria.add(Expression.eq("email", user.getEmail()));
				return criteria.uniqueResult();
			}
			
			
		});
		return u;
	}
}
